package com.rocksdata.mc.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ctz.commonframe.ui.BaseFragment;
import com.ctz.commonframe.ui.view.NoScrollViewPager;
import com.ctz.commonframe.ui.widget.ViewPagerFragmentAdapter;
import com.rocksdata.mc.entity.AnnouncementInfo;
import com.rocksdata.mc.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2016/11/24.
 */
@ContentView(R.layout.fragment_announcement)
public class AnnouncementFragment extends BaseFragment {
    @ViewInject(R.id.view_flag1)
    private View flag1;
    @ViewInject(R.id.view_flag2)
    private View flag2;
    @ViewInject(R.id.et_search)
    private EditText etSearch;
    @ViewInject(R.id.vp_notification)
    private NoScrollViewPager vpNotification;
    @ViewInject(R.id.iv_back)
    private ImageView ivBack;

    private List<Fragment> fragmentList = new ArrayList<>();
    private AnnouncementChildFragment fragment0, fragment1;

//    public interface OnSearchResultListener{
//        void onSearchResult(CharSequence result);
//    }
//    private List<OnSearchResultListener> onSearchResultListenerList = new ArrayList<>();
//
//    public void setOnSearchResultListener(OnSearchResultListener listener){
//        onSearchResultListenerList.add(listener);
//    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Bundle data = getArguments();
        if(data != null && data.getBoolean("showCancel", false)){
            ivBack.setVisibility(View.VISIBLE);
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
        }

//        if(BusinessManager.getUserInfoManager().getCurrentUserType() == UserInfo.USER_TYPE_TEACHER){
//            tvRelease.setVisibility(View.VISIBLE);
//        }
        fragment0 = new AnnouncementChildFragment();
        Bundle data0 = new Bundle();
        data0.putInt("notification_type", AnnouncementInfo.TYPE_NOTICE);
        fragment0.setArguments(data0);
        fragmentList.add(fragment0);

        fragment1 = new AnnouncementChildFragment();
        Bundle data1 = new Bundle();
        data1.putInt("notification_type", AnnouncementInfo.TYPE_PUBLIC_NOTICE);
        fragment1.setArguments(data1);
        fragmentList.add(fragment1);

//        setOnSearchResultListener(fragment0);
//        setOnSearchResultListener(fragment1);

        vpNotification.setAdapter(new ViewPagerFragmentAdapter(getChildFragmentManager(), fragmentList));
        vpNotification.setCurrentItem(0);

//        etSearch.addTextChangedListener(fragment0);

        super.onViewCreated(view, savedInstanceState);
    }

    @Event(value = {R.id.layout_notification_tab1, R.id.layout_notification_tab2})
    private void OnClick(View v) {
        switch (v.getId()) {
            case R.id.layout_notification_tab1:
                flag1.setVisibility(View.VISIBLE);
                flag2.setVisibility(View.GONE);
                vpNotification.setCurrentItem(0);
                etSearch.addTextChangedListener(fragment0);
                break;
            case R.id.layout_notification_tab2:
                flag2.setVisibility(View.VISIBLE);
                flag1.setVisibility(View.GONE);
                vpNotification.setCurrentItem(1);
                etSearch.addTextChangedListener(fragment1);
                break;
        }
    }
}
